package com.tms.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tms.springboot.base.pojo.DGlobalUser;
import com.tms.springboot.mapper.DGlobalUserMapper;
import com.tms.springboot.result.JsonResult;
import com.tms.springboot.result.ResultCode;
import com.tms.springboot.result.ResultTool;
import com.tms.springboot.service.DGlobalUserService;
import com.tms.springboot.users.pojo.PdAddressBook;
import com.tms.springboot.utils.SystemObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 全局账号 前端控制器
 * </p>
 *
 * @author cjj
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/d-global-user")
public class DGlobalUserController {
    @Autowired
    DGlobalUserService dGlobalUserService;

    //注册用户
    @RequestMapping("/adduser")
    public JsonResult updatekdy(@RequestBody DGlobalUser dGlobalUser) {
        QueryWrapper lqw = new QueryWrapper<DGlobalUser>();
        lqw.eq("account", dGlobalUser.getAccount());
        DGlobalUser user = dGlobalUserService.getOne(lqw);
        if (user == null) {
            dGlobalUser.setPassword(SystemObject.getPasswordMd5(dGlobalUser.getPassword()));
            boolean sum = dGlobalUserService.save(dGlobalUser);
            if (sum) {
                return ResultTool.success(ResultCode.SUCCESS);
            } else {
                return ResultTool.fail(ResultCode.COMMON_FAIL);
            }
        } else {
            return ResultTool.fail(ResultCode.USER_ACCOUNT_ALREADY_EXIST);
        }

    }

    //用户登录
    @RequestMapping("/login")
    public JsonResult login(@RequestBody DGlobalUser dGlobalUser) {
        QueryWrapper lqw = new QueryWrapper<DGlobalUser>();
        lqw.eq("account", dGlobalUser.getAccount());
        DGlobalUser user = dGlobalUserService.getOne(lqw);
        if (user == null) {
            return ResultTool.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
        }
        String md5Password = SystemObject.getPasswordMd5(dGlobalUser.getPassword());
        if (user.getPassword().equals(md5Password) == false) {
            return ResultTool.fail(ResultCode.USER_CREDENTIALS_ERROR);
        }
        return ResultTool.success(ResultCode.SUCCESS_login, user);
    }

    //用户修改信息
    @RequestMapping("/update")
    public JsonResult update(@RequestBody DGlobalUser dGlobalUser) {
        if (dGlobalUser.getPassword()!="") {
            dGlobalUser.setPassword(SystemObject.getPasswordMd5(dGlobalUser.getPassword()));
        }else{
            dGlobalUser.setPassword(null);
        }

        dGlobalUserService.updateById(dGlobalUser);
        return ResultTool.success(ResultCode.SUCCESS,dGlobalUser);
    }
}
